package com.comtrade.ilserver.tasks;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonObject;

import android.R.integer;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * User class represents user of a system <br/>
 * example json: <br/>
 * 
 * {	<br/>
 * 		"_id":1, <br/>
 * 		"_uuid":"c319a5c3-0bdc-40dc-b46e-a07fadabd7ec", <br/>
 * 		"firstName":"Initial", <br/>
 * 		"surname":"Administrator", <br/>
 * 		"username":"admin", <br/>
 * 		"roles":["admin"] <br/>
 * }
 */

public class User  implements Parcelable{
	
	public final static String _ID = "_id";
	public final static String _UUID = "_uuid";
	public final static String _FIRSTNAME = "firstName";
	public final static String _SURNAME = "surname";
	public final static String _USERNAME = "username";
	public final static String _ROLES = "roles";
	
	private int id;
	private String uuid;
	private String firstName;
	private String surname;
	private String username;
	private ArrayList<String> role;
	
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public ArrayList<String> getRole() {
		return role;
	}

	public void setRole(ArrayList<String> role) {
		this.role = role;
	
	}
	
	public User (int id, String uuid, String firstname, String surname,
			String username, ArrayList<String> roles) {
		//super();		
		this.id = id;
		this.uuid = uuid;
		this.firstName = firstname;
		this.surname = surname;
		this.username = username;
		this.role = roles;
	}
	

	public User (Parcel source) {
		this.id = source.readInt();
		this.uuid = source.readString();
		this.firstName = source.readString();
		this.surname = source.readString();
		this.username = source.readString();
		source.readStringList(this.role);
	}
	
	public static User fromJSON(String json){
		
		User user = null;
		
		if(json!=null)
		{
			try {
							
				JSONObject korisnik = new JSONObject(json);
				int id = (korisnik.getInt(_ID));
				String uuid = korisnik.getString(_UUID);
				String first = korisnik.getString(_FIRSTNAME);
				String surname = korisnik.getString(_SURNAME);
				String username = korisnik.getString(_USERNAME);
				JSONArray role = korisnik.getJSONArray(_ROLES);
				ArrayList<String> roles = new ArrayList<String>();
				for (int i = 0; i < role.length(); i++) {
					roles.add(role.getString(i));
				}
				
				user = new User(id, uuid, first, surname, username, roles);
				return user;
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public static Parcelable.Creator<User > CREATOR = new Parcelable.Creator<User >() {

		@Override
		public User [] newArray(int size) {
			return new User [size];
		}

		@Override
		public User  createFromParcel(Parcel source) {
			
			return new User (source);
		}
	};
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		//actual object serialization/flattening happens here. You need to individually Parcel each element of the object.		
				//Log.d("writeToParcel...", flags);
			      dest.writeInt(this.getId());
			      dest.writeString(this.getUuid());
			      dest.writeString(this.getFirstName());
			      dest.writeString(this.getSurname());
			      dest.writeString(this.getUsername());
			      dest.writeStringList(this.role);
	}
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}


}
